//
//  VCOfSecond.m
//  17--WeekendHomeWork
//
//  Created by 冀永金 on 16/8/5.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "VCOfSecond.h"

@interface VCOfSecond ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong) NSString *imagePath;
@end

@implementation VCOfSecond

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.name;
    [self config];
    // Do any additional setup after loading the view.
}

- (void)config {
    self.image = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 150, 200)];
    [self.view addSubview:_image];
    _image.backgroundColor = [UIColor redColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handle:)];
    [_image addGestureRecognizer:tap];
    _image.userInteractionEnabled = YES;
    NSUserDefaults *users = [NSUserDefaults standardUserDefaults];
    if ([users dataForKey:@"image"]) {
        [_image setImage:[UIImage imageWithData:[users dataForKey:@"image"]]];
    }

    
    
    
    
    
    UILabel *namelabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 5, 80, 40)];
    [self.view addSubview:namelabel];
    namelabel.text = @"姓名：";
    _textFieldName = [[UITextField alloc]initWithFrame:CGRectMake(245, 5, 115, 40)];
    [self.view addSubview:_textFieldName];
    _textFieldName.placeholder = @"请输入姓名";
    _textFieldName.text = self.name;
    //namelabel.backgroundColor = [UIColor yellowColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(160, 55, 80, 40)];
    [self.view addSubview:label];
    label.text = @"性别：";
    self.genderText = [[UITextField alloc]initWithFrame:CGRectMake(245, 55, 115, 40)];
    [self.view addSubview:self.genderText];
    self.genderText.placeholder = @"请输入性别";
    _genderText.text = self.genderContent;
    UILabel *phoneNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(160, 100, 80, 40)];
    [self.view addSubview:phoneNumLabel];
    //phoneNumLabel.backgroundColor = [UIColor yellowColor];
    phoneNumLabel.text = @"电话号：";
    self.phoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(245, 100, 115, 40)];
    [self.view addSubview:self.phoneTextField];
    self.phoneTextField.placeholder = @"请输入电话号";
    _phoneTextField.text = self.phoneContent;
    UILabel *labelAdress = [[UILabel alloc]initWithFrame:CGRectMake(5, 210, 100, 40)];
    [self.view addSubview:labelAdress];
    labelAdress.text = @"家庭地址：";
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(110, 210, self.view.bounds.size.width - 10 - 80, 40)];
    [self.view addSubview:textView];
    textView.backgroundColor = [UIColor lightGrayColor];
    textView.font = [UIFont systemFontOfSize:20];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:button];
    [button setTitle:@"保存" forState:UIControlStateNormal];
    button.frame = CGRectMake(5, 255, self.view.bounds.size.width - 10, 40);
    [button addTarget:self action:@selector(handleOK:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)handleOK:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:YES];
    [self.delegate getImage:self];
   
    
}

- (void)handle:(UITapGestureRecognizer *)tap {
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:^{
        
    }];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    //保存编辑照片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    if (image != nil) {
        _image.image = image;
    }
    NSData *data;
    if (UIImagePNGRepresentation(image)) {
        data = UIImagePNGRepresentation(image);
    }
    else {
        data = UIImageJPEGRepresentation(image, 1.0);
    }
    [[NSFileManager defaultManager]createFileAtPath:_imagePath contents:data attributes:nil];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:data forKey:@"image"];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
